package it.tukano.treditor.engine.jme3;

import com.jme3.app.SimpleApplication;
import com.jme3.input.InputManager;
import com.jme3.input.MouseInput;
import com.jme3.input.RawInputListener;
import com.jme3.input.event.JoyAxisEvent;
import com.jme3.input.event.JoyButtonEvent;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.input.event.TouchEvent;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class AWTInputProducer implements RawInputListener {
    private final SimpleApplication app;
    private final Component notifier;

    /**
     * Instance initializer
     */
    public AWTInputProducer(Component notifier, SimpleApplication application) {
        this.app = application;
        this.notifier = notifier;
    }

    @Override
    public void beginInput() {
        
    }

    @Override
    public void endInput() {
        
    }

    @Override
    public void onJoyAxisEvent(JoyAxisEvent evt) {
        
    }

    @Override
    public void onJoyButtonEvent(JoyButtonEvent evt) {
        
    }

    @Override
    public void onMouseMotionEvent(MouseMotionEvent evt) {
        final int x = evt.getX();
        final int y = evt.getY();
        final long now = System.currentTimeMillis();
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                MouseEvent e = new MouseEvent(notifier, MouseEvent.MOUSE_MOVED, now, 0, x, notifier.getHeight() - y, 0, false);
                MouseMotionListener[] listeners = notifier.getMouseMotionListeners();
                for (int i = 0; i < listeners.length; i++) {
                    MouseMotionListener listener = listeners[i];
                    listener.mouseMoved(e);
                }
            }
        });
    }

    @Override
    public void onMouseButtonEvent(MouseButtonEvent evt) {
        final int x = evt.getX();
        final int y = evt.getY();
        final long now = System.currentTimeMillis();
        final int buttonIndex = evt.getButtonIndex();
        final boolean released = evt.isReleased();
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                int id = released ? MouseEvent.MOUSE_RELEASED : MouseEvent.MOUSE_PRESSED;
                int button =
                        buttonIndex == MouseInput.BUTTON_LEFT ? MouseEvent.BUTTON1 :
                        buttonIndex == MouseInput.BUTTON_MIDDLE ? MouseEvent.BUTTON2 :
                        MouseEvent.BUTTON3;
                MouseEvent e = new MouseEvent(notifier, id, now, 0, x, notifier.getHeight() - y, 1, false, button);
                MouseListener[] mouseListeners = notifier.getMouseListeners();
                for (int i = 0; i < mouseListeners.length; i++) {
                    MouseListener mouseListener = mouseListeners[i];
                    if(released) mouseListener.mouseReleased(e); else mouseListener.mousePressed(e);
                }
            }
        });
    }

    @Override
    public void onKeyEvent(KeyInputEvent evt) {
        
    }

    @Override
    public void onTouchEvent(TouchEvent evt) {
        
    }
}
